<?php
/*--------------------------------------------------------------------
 度咪科技 - 让工作更轻松快乐
 --------------------------------------------------------------*/

// 后台用户模块
namespace Home\Controller;

use Home\Lib\YCom;

class UserController extends HomeController
{
    protected $config = array('app_type' => 'master');

    public function _search_filter(&$map)
    {
        $keyword = I('keyword');
        if (!empty($keyword)) {
            $map['name|emp_no'] = array('like', '%'.$keyword.'%');
        }
    }

    public function index()
    {
        $plugin['date'] = true;
        $this->assign('plugin', $plugin);

        $model = M('Position');
        $list = $model->where('is_del=0')->order('sort asc')->getField('id,name');
        $this->assign('position_list', $list);

        $model = M('Dept');
        $list = $model->where('is_del=0')->order('sort asc')->getField('id,name');
        $this->assign('dept_list', $list);

        if (I('param.eq_is_del')) {
            $eq_is_del = I('param.eq_is_del');
        } else {
            $eq_is_del = '0';
        }
        //die;
        $this->assign('eq_is_del', $eq_is_del);
        $this->assign('position_id', I('param.position_id'));

        $map = $this->_search();
        if (method_exists($this, '_search_filter')) {
            $this->_search_filter($map);
        }
        $map['is_del'] = array('eq', $eq_is_del);
        if (I('param.position_id')) {
            $map['position_id'] = I('param.position_id');
        }

        $model = D('User');

        if (!empty($model)) {
            $this->_list($model, $map, 'emp_no');
        }

        $this->display();
    }

    public function add()
    {
        $plugin['date'] = true;
        $this->assign('plugin', $plugin);

        $model = M('Position');
        $list = $model->where('is_del=0')->order('sort asc')->getField('id,name');
        $this->assign('position_list', $list);

        $model = M('Dept');
        $list = $model->where('is_del=0')->order('sort asc')->getField('id,name');
        $this->assign('dept_list', $list);

        $this->display();
    }

    // 检查帐号
    public function check_account()
    {
        if (!preg_match('/^[a-z]\w{4,}$/i', $_POST['emp_no'])) {
            $this->error('用户名必须是字母，且5位以上！');
        }
        $User = M('User');
        // 检测用户名是否冲突
        $name = I('emp_no');
        $result = $User->getByAccount($name);
        if ($result) {
            $this->error('该编码已经存在！');
        } else {
            $this->assign('jumpUrl', get_return_url());
            $this->success('该编码可以使用！');
        }
    }

    // 插入数据
    protected function _insert($name = 'User')
    {
        // 创建数据对象
        $model = D('User');
        if (!$model->create()) {
            $this->error($model->getError());
        } else {
            // 写入帐号数据
            $model->letter = get_letter($model->name);
            $model->password = md5($model->emp_no);
            $emp_no = $model->emp_no;
            $name = $model->name;
            $mobile_tel = $model->mobile_tel;
            if ($result = $model->add()) {
                $this->add_default_role($result);

                $this->assign('jumpUrl', get_return_url());
                $this->success('用户添加成功！');
            } else {
                $this->error('用户添加失败！');
            }
        }
    }

    protected function _update($name = 'User')
    {
        $model = D($name);
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        // 更新数据
        $model->__set('letter', get_letter($model->__get('name')));
        $emp_no = $model->emp_no;
        $name = $model->name;
        $mobile_tel = $model->mobile_tel;
        $list = $model->save();
        if (false !== $list) {
            //成功提示
            $this->assign('jumpUrl', get_return_url());
            $this->success('编辑成功!');
        } else {
            //错误提示
            $this->error('编辑失败!');
        }
    }

    protected function add_default_role($user_id)
    {
        //新增用户自动加入相应权限组
        $RoleUser = M('RoleUser');
        $RoleUser->user_id = $user_id;
        // 默认加入权限
        $RoleUser->role_id = C('DEFAULT_ROLE');
        $RoleUser->add();
    }

    //重置密码
    public function reset_pwd()
    {
        $id = $_POST['user_id'];
        $password = $_POST['password'];
        if ('' == trim($password)) {
            $this->error('密码不能为空!');
        }
        $User = M('User');
        $User->password = md5($password);
        $User->id = $id;
        $result = $User->save();
        if (false !== $result) {
            $this->assign('jumpUrl', get_return_url());
            $this->success('密码修改成功');
        } else {
            $this->error('重置密码失败！');
        }
    }

    public function password()
    {
        $this->assign('id', I('id'));
        $this->display();
    }

    public function json()
    {
        header('Content-Type:text/html; charset=utf-8');
        $key = $_REQUEST['key'];

        $model = M('User');
        $where['name'] = array('like', '%'.$key.'%');
        $where['emp_no'] = array('like', '%'.$key.'%');
        $where['_logic'] = 'or';
        $map['_complex'] = $where;
        $list = $model->where($map)->field('id,name')->select();
        exit(json_encode($list));
    }

    public function del()
    {
        $id = I('user_id');
        $this->_destory($id);
    }

    public function add_role($user_id, $role_list)
    {
        $role_list = explode(',', $role_list);
        $role_list = array_filter($role_list);
        $RoleUser = M('RoleUser');
        $RoleUser->user_id = $user_id;
        foreach ($role_list as $role_id) {
            $RoleUser->role_id = $role_id;
            $RoleUser->add();
        }
    }

    public function sync()
    {
        $qywechat = getQyWechatObj();

        $result = $qywechat->getDepartments(
            C('QYWECHAT')['department']
        );

        $deptModel = M("Dept");

        $deptCount = 0;
        foreach ((array) $result as $department) {
            if ($deptModel ->add([
                'id' => $department['id'],
                'name' => $department['name'],
                'pid' => $department['parentid'],
                'sort' => $department['order'],
            ], [], true)) {
                ++$deptCount;
            }
        }

        $members = [];
        $userModel = M('User');

        $result = $qywechat->getDepartmentMembers(
            C('QYWECHAT')['department'],
            true,
            true
        );

        foreach ((array) $result as $user) {
            if ($userModel->where(['emp_no' => ['eq', $user['userid']]])->select()) {
                continue;
            }

            $members[] = [
                'name' => $user['name'],
                'emp_no' => $user['userid'],
                'dept_id' => YCom::issetor($user['department'][0], 0),
                'mobile_tel' => YCom::issetor($user['mobile'], ''),
                'email' => YCom::issetor($user['email'], ''),
                'pic' => YCom::issetor($user['avatar'], ''),
                'password' => md5(time().rand(1, 50000)),
            ];
        }

        $count = 0;
        foreach ($members as $member) {
            if ($userModel->add($member, [], true)) {
                ++$count;
            }
        }

        die("成功更新部门 {$deptCount} 条，新增用户 {$count} 条");
    }
}
